const { regeneratorRuntime, md5 } = global

Page(global.connect(
  state => ({
    ...state.globalReducer,
    ...state.profileReducer
  }),
  dispatch => ({
    // 身份验证
    async handleSubmit(e) {
      console.log('handleSubmit', e)

      const {
        id, name, certid, service_area, province, city
      } = this.data.minaLoginRes.data

      const formData = {
        cert_id: certid,
        id, name, province, city, service_area
      }
      console.log(formData)
      // 简单校验
      if (!name || !certid || !province || !city || !service_area) {
        this.showToast({ message: '请填写完整信息' })
        return
      }

      // 注册
      try {
        const actionRes = await dispatch(global.profileReducer.userAuth(formData))

        console.log(actionRes)
        if (actionRes.actionStatus === 'SUCCESS') {
          this.selectComponent('#toast').handleShow({
            type: 'success',
            message: actionRes.apiMes
          })
          setTimeout(() => {
            global.handleRoute({ route: 'login', type: 'redirectTo' })
          }, 1000);
        } else {
          this.selectComponent('#toast').handleShow({
            message: actionRes.apiMes || '注册失败，请稍后再试'
          })
        }
      } catch (error) {
        this.selectComponent('#toast').handleShow({
          message: error.apiMes || '注册失败，请稍后再试'
        })
      }
    },
    asyncAuthInfo(data) {
      dispatch(global.globalReducer.asyncAuthInfo(data))
    }
  })
)({
  data: {
    // ---
    multiArray: [
      ['广东省', '湖南省'],
      ['深圳市', '惠州市', '广州市', '佛山市', '中山市']
    ],
    multiIndex: [0, 0],
    prevMultiIndex: [0, 0],
    objectMultiArray: [
      [
        { id: 0, name: '广东省' },
        { id: 1, name: '湖南省' }
      ],
      [
        { id: 0, name: '深圳市', province_id: 0 },
        { id: 1, name: '惠州市', province_id: 0 },
        { id: 2, name: '广州市', province_id: 0 },
        { id: 3, name: '佛山市', province_id: 0 },
        { id: 3, name: '中山市', province_id: 0 },

        { id: 4, name: 'h1', province_id: 1 },
        { id: 5, name: 'h2', province_id: 1 },
        { id: 6, name: 'h3', province_id: 1 },
        { id: 7, name: 'h4', province_id: 1 },
        { id: 8, name: 'h5', province_id: 1 },
      ]
    ],
    // ---
    areas: ['南山区', '宝安区', '福田区', '罗湖区', '龙华区', '龙岗区', '大鹏新区', '坪山区'],
    areaIndex: 1,
  },
  onLoad() {
    const index = this.data.areas.indexOf(this.data.minaLoginRes.data.service_area)
    this.setData({
      areaIndex: index !== -1 ? index : 0
    })
  },
  showToast({ type, message }) {
    this.selectComponent('#toast').handleShow({ type, message })
  },
  handleField(e) {
    const obj = {}
    obj[e.currentTarget.dataset.field] = e.detail.value
    this.asyncAuthInfo(obj)
  },
  handleLicon1Tap(e) {
    wx.navigateBack()
    global.handleRoute({ route: 'login', type: 'redirectTo' })
  },
  bindMultiPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
    const { multiArray, multiIndex } = this.data
    this.asyncAuthInfo({
      province: multiArray[0][multiIndex[0]],
      city: multiArray[1][multiIndex[1]],
    })
  },
  bindMultiPickerColumnChange(e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value)
    const { multiArray, multiIndex, objectMultiArray } = this.data

    // 列
    switch (e.detail.column) {
      case 0:
        multiArray[1] = objectMultiArray[1].filter(i => i.province_id === e.detail.value).map(i => i.name)
        multiIndex[1] = 0
        break;
      case 1:
        break;
      default:
        break;
    }
    multiIndex[e.detail.column] = e.detail.value
    // console.log(multiIndex, multiArray)
    this.setData({ multiArray, multiIndex })
  },
  handleAreaChange(e) {
    console.log('handleAreaChange', e)
    const areaIndex = Number(e.detail.value) || 0
    this.setData({ areaIndex })
    this.asyncAuthInfo({
      service_area: this.data.areas[areaIndex]
    })
  }
}))
